library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 10.5
# Treatment DOV_LEAD - 1 Civ 2 Dem 3 Rep

# 1&2 = Agree; 4&5 = Disagree
# Q24A - Political leaders had a good plan
# Q24B - Military leaders implemented as well as they could
# Q24C - Military leaders gave good advice about strategy
# Q24D - Pol leaders listened to mil leaders as much as they should
# Q24E - Pol leaders did a good job integrating DIME in IRQ and AFG
# Q24F - Mil leaders did a good job integrating ops with Dip Econ in I&A

df2 <- df
df2 <- df2[df2$Q11 < 77,]

summary(factor(df2$DOV_LEAD))
# Panel 1
summary(factor(df2$Q24A))
summary(factor(df2$Q24B))

dfp1 <- df2[df2$Q24A < 6 & df2$Q24B < 6,]

dfp1$Q24AT <- 0
dfp1$Q24AT[dfp1$Q24A < 3] <- 1

dfp1$Q24BT <- 0
dfp1$Q24BT[dfp1$Q24B < 3] <- 1

# Panel 2
summary(factor(df2$Q24D))
summary(factor(df2$Q24C))

dfp2 <- df2[df2$Q24D < 6 & df2$Q24C < 6,]

dfp2$Q24DT <- 0
dfp2$Q24DT[dfp2$Q24D < 3] <- 1

dfp2$Q24CT <- 0
dfp2$Q24CT[dfp2$Q24C < 3] <- 1

# Panel 3
summary(factor(df2$Q24E))
summary(factor(df2$Q24F))

dfp3 <- df2[df2$Q24E < 6 & df2$Q24F < 6,]

dfp3$Q24ET <- 0
dfp3$Q24ET[dfp3$Q24E < 3] <- 1

dfp3$Q24FT <- 0
dfp3$Q24FT[dfp3$Q24F < 3] <- 1


# Survey designs
w2_design_p1 <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = dfp1
  )

w2_design_p2 <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = dfp2
  )

w2_design_p3 <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = dfp3
  )

# Transgender ban
cp1 <- svyby(~Q24AT,~DOV_LEAD+party, w2_design_p1, svymean, na.rm = TRUE)
mp1 <- svyby(~Q24BT,~party, w2_design_p1, svymean, na.rm = TRUE)
mp1$DOV_LEAD <- 4
colnames(cp1)[3] <- "agreement"
colnames(mp1)[2] <- "agreement"
p1 <- rbind(cp1,mp1)

cp2 <- svyby(~Q24DT,~DOV_LEAD+party, w2_design_p2, svymean, na.rm = TRUE)
mp2 <- svyby(~Q24CT,~party, w2_design_p2, svymean, na.rm = TRUE)
mp2$DOV_LEAD <- 4
colnames(cp2)[3] <- "agreement"
colnames(mp2)[2] <- "agreement"
p2 <- rbind(cp2,mp2)

cp3 <- svyby(~Q24ET,~DOV_LEAD+party, w2_design_p3, svymean, na.rm = TRUE)
mp3 <- svyby(~Q24FT,~party, w2_design_p3, svymean, na.rm = TRUE)
mp3$DOV_LEAD <- 4
colnames(cp3)[3] <- "agreement"
colnames(mp3)[2] <- "agreement"
p3 <- rbind(cp3,mp3)


# Build out plots
# plot 1
pt.est1 <- c(p1$agreement[p1$party==0&p1$DOV_LEAD==1], 
            p1$agreement[p1$party==0&p1$DOV_LEAD==2],
            p1$agreement[p1$party==0&p1$DOV_LEAD==3],
            p1$agreement[p1$party==0&p1$DOV_LEAD==4],
            0,
            p1$agreement[p1$party==1&p1$DOV_LEAD==1], 
            p1$agreement[p1$party==1&p1$DOV_LEAD==2],
            p1$agreement[p1$party==1&p1$DOV_LEAD==3],
            p1$agreement[p1$party==1&p1$DOV_LEAD==4],
            0,
            p1$agreement[p1$party==2&p1$DOV_LEAD==1], 
            p1$agreement[p1$party==2&p1$DOV_LEAD==2],
            p1$agreement[p1$party==2&p1$DOV_LEAD==3],
            p1$agreement[p1$party==2&p1$DOV_LEAD==4])
ci.low1 <- c(p1$agreement[p1$party==0&p1$DOV_LEAD==1]-1.96*p1$se[p1$party==0&p1$DOV_LEAD==1], 
            p1$agreement[p1$party==0&p1$DOV_LEAD==2]-1.96*p1$se[p1$party==0&p1$DOV_LEAD==2],
            p1$agreement[p1$party==0&p1$DOV_LEAD==3]-1.96*p1$se[p1$party==0&p1$DOV_LEAD==3],
            p1$agreement[p1$party==0&p1$DOV_LEAD==4]-1.96*p1$se[p1$party==0&p1$DOV_LEAD==4],
            0,
            p1$agreement[p1$party==1&p1$DOV_LEAD==1]-1.96*p1$se[p1$party==1&p1$DOV_LEAD==1], 
            p1$agreement[p1$party==1&p1$DOV_LEAD==2]-1.96*p1$se[p1$party==1&p1$DOV_LEAD==2],
            p1$agreement[p1$party==1&p1$DOV_LEAD==3]-1.96*p1$se[p1$party==1&p1$DOV_LEAD==3],
            p1$agreement[p1$party==1&p1$DOV_LEAD==4]-1.96*p1$se[p1$party==1&p1$DOV_LEAD==4],
            0,
            p1$agreement[p1$party==2&p1$DOV_LEAD==1]-1.96*p1$se[p1$party==2&p1$DOV_LEAD==1], 
            p1$agreement[p1$party==2&p1$DOV_LEAD==2]-1.96*p1$se[p1$party==2&p1$DOV_LEAD==2],
            p1$agreement[p1$party==2&p1$DOV_LEAD==3]-1.96*p1$se[p1$party==2&p1$DOV_LEAD==3],
            p1$agreement[p1$party==2&p1$DOV_LEAD==4]-1.96*p1$se[p1$party==2&p1$DOV_LEAD==4])
ci.high1 <- c(p1$agreement[p1$party==0&p1$DOV_LEAD==1]+1.96*p1$se[p1$party==0&p1$DOV_LEAD==1], 
             p1$agreement[p1$party==0&p1$DOV_LEAD==2]+1.96*p1$se[p1$party==0&p1$DOV_LEAD==2],
             p1$agreement[p1$party==0&p1$DOV_LEAD==3]+1.96*p1$se[p1$party==0&p1$DOV_LEAD==3],
             p1$agreement[p1$party==0&p1$DOV_LEAD==4]+1.96*p1$se[p1$party==0&p1$DOV_LEAD==4],
             0,
             p1$agreement[p1$party==1&p1$DOV_LEAD==1]+1.96*p1$se[p1$party==1&p1$DOV_LEAD==1], 
             p1$agreement[p1$party==1&p1$DOV_LEAD==2]+1.96*p1$se[p1$party==1&p1$DOV_LEAD==2],
             p1$agreement[p1$party==1&p1$DOV_LEAD==3]+1.96*p1$se[p1$party==1&p1$DOV_LEAD==3],
             p1$agreement[p1$party==1&p1$DOV_LEAD==4]+1.96*p1$se[p1$party==1&p1$DOV_LEAD==4],
             0,
             p1$agreement[p1$party==2&p1$DOV_LEAD==1]+1.96*p1$se[p1$party==2&p1$DOV_LEAD==1], 
             p1$agreement[p1$party==2&p1$DOV_LEAD==2]+1.96*p1$se[p1$party==2&p1$DOV_LEAD==2],
             p1$agreement[p1$party==2&p1$DOV_LEAD==3]+1.96*p1$se[p1$party==2&p1$DOV_LEAD==3],
             p1$agreement[p1$party==2&p1$DOV_LEAD==4]+1.96*p1$se[p1$party==2&p1$DOV_LEAD==4])
plot.labs1 <- c("Civilian","Democratic","Republican","Military","",
               "Civilian ","Democratic ","Republican ","Military "," ",
               " Civilian"," Democratic"," Republican"," Military")

df.plot1 <- data.frame(Treatment=plot.labs1, estimate=pt.est1, 
                      lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs1)

g.p1 <- ggplot(data=df.plot1, 
              aes(x=Treatment, y=estimate),
              size = 0.5)+
  geom_col(position=position_dodge(1), width=0.75)+
  geom_errorbar(aes(ymin=lower,ymax=upper),width=0.2)+
  ylim(0,.65)+
  ylab("Respondent Agreement (%)")+
  xlab("\nDemocratic Respondents                                                                            Independent Respondents                                                                           Republican Respondents")+
  ggtitle("Political leaders had a good plan/Military leaders implemented plan well")+
  geom_text(aes(label = round(estimate,3)), vjust = -0.5)

# plot 2
pt.est2 <- c(p2$agreement[p2$party==0&p2$DOV_LEAD==1], 
            p2$agreement[p2$party==0&p2$DOV_LEAD==2],
            p2$agreement[p2$party==0&p2$DOV_LEAD==3],
            p2$agreement[p2$party==0&p2$DOV_LEAD==4],
            0,
            p2$agreement[p2$party==1&p2$DOV_LEAD==1], 
            p2$agreement[p2$party==1&p2$DOV_LEAD==2],
            p2$agreement[p2$party==1&p2$DOV_LEAD==3],
            p2$agreement[p2$party==1&p2$DOV_LEAD==4],
            0,
            p2$agreement[p2$party==2&p2$DOV_LEAD==1], 
            p2$agreement[p2$party==2&p2$DOV_LEAD==2],
            p2$agreement[p2$party==2&p2$DOV_LEAD==3],
            p2$agreement[p2$party==2&p2$DOV_LEAD==4])
ci.low2 <- c(p2$agreement[p2$party==0&p2$DOV_LEAD==1]-1.96*p2$se[p2$party==0&p2$DOV_LEAD==1], 
            p2$agreement[p2$party==0&p2$DOV_LEAD==2]-1.96*p2$se[p2$party==0&p2$DOV_LEAD==2],
            p2$agreement[p2$party==0&p2$DOV_LEAD==3]-1.96*p2$se[p2$party==0&p2$DOV_LEAD==3],
            p2$agreement[p2$party==0&p2$DOV_LEAD==4]-1.96*p2$se[p2$party==0&p2$DOV_LEAD==4],
            0,
            p2$agreement[p2$party==1&p2$DOV_LEAD==1]-1.96*p2$se[p2$party==1&p2$DOV_LEAD==1], 
            p2$agreement[p2$party==1&p2$DOV_LEAD==2]-1.96*p2$se[p2$party==1&p2$DOV_LEAD==2],
            p2$agreement[p2$party==1&p2$DOV_LEAD==3]-1.96*p2$se[p2$party==1&p2$DOV_LEAD==3],
            p2$agreement[p2$party==1&p2$DOV_LEAD==4]-1.96*p2$se[p2$party==1&p2$DOV_LEAD==4],
            0,
            p2$agreement[p2$party==2&p2$DOV_LEAD==1]-1.96*p2$se[p2$party==2&p2$DOV_LEAD==1], 
            p2$agreement[p2$party==2&p2$DOV_LEAD==2]-1.96*p2$se[p2$party==2&p2$DOV_LEAD==2],
            p2$agreement[p2$party==2&p2$DOV_LEAD==3]-1.96*p2$se[p2$party==2&p2$DOV_LEAD==3],
            p2$agreement[p2$party==2&p2$DOV_LEAD==4]-1.96*p2$se[p2$party==2&p2$DOV_LEAD==4])
ci.high2 <- c(p2$agreement[p2$party==0&p2$DOV_LEAD==1]+1.96*p2$se[p2$party==0&p2$DOV_LEAD==1], 
             p2$agreement[p2$party==0&p2$DOV_LEAD==2]+1.96*p2$se[p2$party==0&p2$DOV_LEAD==2],
             p2$agreement[p2$party==0&p2$DOV_LEAD==3]+1.96*p2$se[p2$party==0&p2$DOV_LEAD==3],
             p2$agreement[p2$party==0&p2$DOV_LEAD==4]+1.96*p2$se[p2$party==0&p2$DOV_LEAD==4],
             0,
             p2$agreement[p2$party==1&p2$DOV_LEAD==1]+1.96*p2$se[p2$party==1&p2$DOV_LEAD==1], 
             p2$agreement[p2$party==1&p2$DOV_LEAD==2]+1.96*p2$se[p2$party==1&p2$DOV_LEAD==2],
             p2$agreement[p2$party==1&p2$DOV_LEAD==3]+1.96*p2$se[p2$party==1&p2$DOV_LEAD==3],
             p2$agreement[p2$party==1&p2$DOV_LEAD==4]+1.96*p2$se[p2$party==1&p2$DOV_LEAD==4],
             0,
             p2$agreement[p2$party==2&p2$DOV_LEAD==1]+1.96*p2$se[p2$party==2&p2$DOV_LEAD==1], 
             p2$agreement[p2$party==2&p2$DOV_LEAD==2]+1.96*p2$se[p2$party==2&p2$DOV_LEAD==2],
             p2$agreement[p2$party==2&p2$DOV_LEAD==3]+1.96*p2$se[p2$party==2&p2$DOV_LEAD==3],
             p2$agreement[p2$party==2&p2$DOV_LEAD==4]+1.96*p2$se[p2$party==2&p2$DOV_LEAD==4])
plot.labs2 <- c("Civilian","Democratic","Republican","Military","",
               "Civilian ","Democratic ","Republican ","Military "," ",
               " Civilian"," Democratic"," Republican"," Military")

df.plot2 <- data.frame(Treatment=plot.labs2, estimate=pt.est2, 
                      lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs2)

g.p2 <- ggplot(data=df.plot2, 
               aes(x=Treatment, y=estimate),
               size = 0.5)+
  geom_col(position=position_dodge(1), width=0.75)+
  geom_errorbar(aes(ymin=lower,ymax=upper),width=0.2)+
  ylim(0,.65)+
  ylab("Respondent Agreement (%)")+
  xlab("\nDemocratic Respondents                                                                            Independent Respondents                                                                           Republican Respondents")+
  ggtitle("Political leaders listened to military advice/Military leaders gave good advice")+
  geom_text(aes(label = round(estimate,3)), vjust = -0.5)

# plot 3
pt.est3 <- c(p3$agreement[p3$party==0&p3$DOV_LEAD==1], 
            p3$agreement[p3$party==0&p3$DOV_LEAD==2],
            p3$agreement[p3$party==0&p3$DOV_LEAD==3],
            p3$agreement[p3$party==0&p3$DOV_LEAD==4],
            0,
            p3$agreement[p3$party==1&p3$DOV_LEAD==1], 
            p3$agreement[p3$party==1&p3$DOV_LEAD==2],
            p3$agreement[p3$party==1&p3$DOV_LEAD==3],
            p3$agreement[p3$party==1&p3$DOV_LEAD==4],
            0,
            p3$agreement[p3$party==2&p3$DOV_LEAD==1], 
            p3$agreement[p3$party==2&p3$DOV_LEAD==2],
            p3$agreement[p3$party==2&p3$DOV_LEAD==3],
            p3$agreement[p3$party==2&p3$DOV_LEAD==4])
ci.low3 <- c(p3$agreement[p3$party==0&p3$DOV_LEAD==1]-1.96*p3$se[p3$party==0&p3$DOV_LEAD==1], 
            p3$agreement[p3$party==0&p3$DOV_LEAD==2]-1.96*p3$se[p3$party==0&p3$DOV_LEAD==2],
            p3$agreement[p3$party==0&p3$DOV_LEAD==3]-1.96*p3$se[p3$party==0&p3$DOV_LEAD==3],
            p3$agreement[p3$party==0&p3$DOV_LEAD==4]-1.96*p3$se[p3$party==0&p3$DOV_LEAD==4],
            0,
            p3$agreement[p3$party==1&p3$DOV_LEAD==1]-1.96*p3$se[p3$party==1&p3$DOV_LEAD==1], 
            p3$agreement[p3$party==1&p3$DOV_LEAD==2]-1.96*p3$se[p3$party==1&p3$DOV_LEAD==2],
            p3$agreement[p3$party==1&p3$DOV_LEAD==3]-1.96*p3$se[p3$party==1&p3$DOV_LEAD==3],
            p3$agreement[p3$party==1&p3$DOV_LEAD==4]-1.96*p3$se[p3$party==1&p3$DOV_LEAD==4],
            0,
            p3$agreement[p3$party==2&p3$DOV_LEAD==1]-1.96*p3$se[p3$party==2&p3$DOV_LEAD==1], 
            p3$agreement[p3$party==2&p3$DOV_LEAD==2]-1.96*p3$se[p3$party==2&p3$DOV_LEAD==2],
            p3$agreement[p3$party==2&p3$DOV_LEAD==3]-1.96*p3$se[p3$party==2&p3$DOV_LEAD==3],
            p3$agreement[p3$party==2&p3$DOV_LEAD==4]-1.96*p3$se[p3$party==2&p3$DOV_LEAD==4])
ci.high3 <- c(p3$agreement[p3$party==0&p3$DOV_LEAD==1]+1.96*p3$se[p3$party==0&p3$DOV_LEAD==1], 
             p3$agreement[p3$party==0&p3$DOV_LEAD==2]+1.96*p3$se[p3$party==0&p3$DOV_LEAD==2],
             p3$agreement[p3$party==0&p3$DOV_LEAD==3]+1.96*p3$se[p3$party==0&p3$DOV_LEAD==3],
             p3$agreement[p3$party==0&p3$DOV_LEAD==4]+1.96*p3$se[p3$party==0&p3$DOV_LEAD==4],
             0,
             p3$agreement[p3$party==1&p3$DOV_LEAD==1]+1.96*p3$se[p3$party==1&p3$DOV_LEAD==1], 
             p3$agreement[p3$party==1&p3$DOV_LEAD==2]+1.96*p3$se[p3$party==1&p3$DOV_LEAD==2],
             p3$agreement[p3$party==1&p3$DOV_LEAD==3]+1.96*p3$se[p3$party==1&p3$DOV_LEAD==3],
             p3$agreement[p3$party==1&p3$DOV_LEAD==4]+1.96*p3$se[p3$party==1&p3$DOV_LEAD==4],
             0,
             p3$agreement[p3$party==2&p3$DOV_LEAD==1]+1.96*p3$se[p3$party==2&p3$DOV_LEAD==1], 
             p3$agreement[p3$party==2&p3$DOV_LEAD==2]+1.96*p3$se[p3$party==2&p3$DOV_LEAD==2],
             p3$agreement[p3$party==2&p3$DOV_LEAD==3]+1.96*p3$se[p3$party==2&p3$DOV_LEAD==3],
             p3$agreement[p3$party==2&p3$DOV_LEAD==4]+1.96*p3$se[p3$party==2&p3$DOV_LEAD==4])
plot.labs3 <- c("Civilian","Democratic","Republican","Military","",
               "Civilian ","Democratic ","Republican ","Military "," ",
               " Civilian"," Democratic"," Republican"," Military")

df.plot3 <- data.frame(Treatment=plot.labs3, estimate=pt.est3, 
                      lower=ci.low3, upper=ci.high3)
df.plot3$Treatment <- factor(df.plot3$Treatment, levels = plot.labs3)

g.p3 <- ggplot(data=df.plot3, 
               aes(x=Treatment, y=estimate),
               size = 0.5)+
  geom_col(position=position_dodge(1), width=0.75)+
  geom_errorbar(aes(ymin=lower,ymax=upper),width=0.2)+
  ylim(0,.65)+
  ylab("Respondent Agreement (%)")+
  xlab("\nDemocratic Respondents                                                                            Independent Respondents                                                                           Republican Respondents")+
  ggtitle("Political/Military leaders did a good job integrating military, diplomatic, and economic efforts")+
  geom_text(aes(label = round(estimate,3)), vjust = -0.5)

grid.arrange(g.p1,g.p2,g.p3,ncol=1,nrow=3)
